<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <!--
            :inline="true"：行内表单
            :model="searchForm" 双向绑定公共前缀
            -->
            <el-form :inline="true" :model="searchForm">
                <el-form-item label="服务">
                    <el-input v-model="searchForm.name" placeholder="服务"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="searchForm.state" clearable  placeholder="请选择状态">
                        <el-option label="上架" value="1"></el-option>
                        <el-option label="下架" value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="searchFormProducts" >查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                    <el-button type="primary" :disabled="this.sels.length===0" @click="handleOnsale">上架</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <!--列表-->
        <el-table :data="products"  @selection-change="selsChange"  v-loading="listLoading" style="width: 100%;">
            <!--显示多选框-->
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column prop="name" label="服务名" >
            </el-table-column>
            <el-table-column prop="saleprice" label="售价" >
            </el-table-column>
            <el-table-column prop="costprice" label="成本价" >
            </el-table-column>
            <el-table-column prop="offsaletime" label="下架时间" >
            </el-table-column>
            <el-table-column prop="onsaletime" label="上架时间" >
            </el-table-column>
            <el-table-column prop="createtime" label="创建时间" >
            </el-table-column>

            <el-table-column prop="state" label="状态">
                <template scope="scope">
                    <span style="color: green" v-if="scope.row.state==1">上架</span>
                    <span style="color: red" v-else-if="scope.row.state==0">下架</span>
                </template>
            </el-table-column>
            <el-table-column prop="salecount" label="销量" >
            </el-table-column>
            <el-table-column label="操作" >
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>


        <!--新增/修改弹出框-->
        <el-dialog :title="title" :visible.sync="addFormVisible" >
            <!--
                ref:简单理解就是申明form表单的id
                :rules:定义校验规则
             -->
            <el-form ref="form" :rules="rules"  :model="form" label-width="80px">
                <el-form-item label="服务" prop="name" >
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item prop="logo" label="服务图片">
                    <el-upload
                            class="upload-demo"
                            action="/api/fastdfs/upload"
                            :on-remove="handleRemove"
                            :on-success="handleSuccess"
                            :file-list="fileList"
                            list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="成本价">
                    <el-input v-model="form.costprice"></el-input>
                </el-form-item>
                <el-form-item label="售价">
                    <el-input v-model="form.saleprice"></el-input>
                </el-form-item>
                <el-form-item label="服务详情">
                   <!-- <el-input v-model="form.intro"></el-input>-->
                    <quill-editor :options="editorOption" v-model="form.intro">
                    </quill-editor>
                </el-form-item>
                <el-form-item label="预约须知">
                    <!--<el-input v-model="form.orderNotice"></el-input>-->
                    <quill-editor :options="editorOption" v-model="form.orderNotice">
                    </quill-editor>
                </el-form-item>


            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click="addSubmit" >提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>
<script>
    import { quillEditor } from "vue-quill-editor"; //调用编辑器
    import 'quill/dist/quill.core.css';
    import 'quill/dist/quill.snow.css';
    import 'quill/dist/quill.bubble.css';
    import quillConfig from "../../common/quill-config"
    export default {
        //申明组件
        components: {
            quillEditor//quillEditor:quillEditor
        },
        data() {
            return {
                editorOption: quillConfig,
                //图片回显
                fileList:[],
                /*定义校验规则*/
                rules: {
                    name: [
                        {required: true, message: '部门名必填!!', trigger: 'blur'}
                    ]
                },
                //弹出框标题
                title:"",
                //弹出框默认是关闭状态
                addFormVisible:false,
                //高级查询form表单
                searchForm:{
                    //部门名
                    name:"",
                    //状态
                    state:""
                },
                //弹出框form中的属性
                form:{
                    id:"",
                    name:"",//部门名
                    resources:"",//图片资源
                    costprice:"",//成本价
                    saleprice:"",//售价
                    intro:"",//详情
                    orderNotice:""//预约须知
                },
                //分页列表
                products: [],
                //显示加载框
                listLoading: false,
                //记录复选框选中多少行
                sels: [],
                //当前页
                currentPage: 1,
                //每页条数
                pageSize: 10,
                //总条数
                total: 0
            };
        },
        methods:{
            //上架事件
            handleOnsale(){
                this.$confirm('确认上架吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.post("/product/onsale",this.sels).then(res => {
                        let {success, message} = res.data;
                        if(success){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            //重新刷新页面
                            this.loadProducts();
                        }else{
                            this.$message.error(message);
                        }
                    });
                }).catch(() => {
                });

            },
            //先不管，下午说
            handleSuccess(response, file, fileList){
                console.debug(fileList);
                // for(var i=0;i<fileList.length;i++){
                //     if(i==0){
                //         this.form.resources = fileList[i].response.resultObj;
                //     }else{
                //         this.form.resources = this.form.resources+","+ fileList[i].response.resultObj;
                //     }
                // }
            },
            //先不管，下午说
            handleRemove(file, fileList) {
                console.debug(file,fileList);

                // var filePath =file.response.resultObj;
                // this.$http.delete("/common/fastDfs/?path="+filePath)
                //     .then(res=>{
                //         if(res.data.success){
                //             this.$message({
                //                 message: '删除成功!',
                //                 type: 'success'
                //             });
                //         }else{
                //             this.$message({
                //                 message: '删除失败!',
                //                 type: 'error'
                //             });
                //         }
                //     })
            },
            //选中复选框触发函数
            selsChange(rows){
                this.sels = rows;
            },
            //点击编辑框触发函数
            handleEdit(index,row){

                console.debug(row);
                //修改标题
                this.title = "修改";
                //回显数据（给普通属性赋值）
                for(let k in this.form){
                    this.form[k] = row[k];
                }
                //如果服务详情有值，预约须知和详情就进行回显
                if(row.productDetail){
                    this.form.orderNotice = row.productDetail.orderNotice;
                    this.form.intro = row.productDetail.intro;
                }
                this.fileList = [];
                //附件要进行回显
                if(row.resources){
                    var images = row.resources.split(",");
                    for(var i=0;i<images.length;i++){
                        this.fileList.push({"url":this.$fastBaseUrl+images[i]});
                    }
                }
                //显示弹出框
                this.addFormVisible = true;
            },
            //点击删除按钮触发函数
            handleDel(index,row){
                this.$confirm('您确认离我而去吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.delete("/product/" + row.id).then(res => {
                        let {success, message} = res.data;
                        if(success){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            //重新刷新页面
                            this.loadProducts();
                        }else{
                            this.$message.error(message);
                        }
                    });
                }).catch(() => {
                });
            },
            //批量删除触发函数
            batchRemove(){
                this.$confirm('您确认离我而去吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.post("/product/batchRemove",this.sels).then(res => {
                        let {success, message} = res.data;
                        if(success){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            //重新刷新页面
                            this.loadProducts();
                        }else{
                            this.$message.error(message);
                        }
                    });
                }).catch(() => {
                });
            },
            //页码发生改变的时候触发函数
            handleCurrentChange(val){
                //重新修改页码
                this.currentPage = val;
                //重新加载列表
                this.loadProducts();
            },
            //加载员工分页列表
            loadProducts(){
                //分页传递参数
                let param = {
                    "currentPage": this.currentPage,
                    "pageSize": this.pageSize,
                    "name":this.searchForm.name,
                    "state":this.searchForm.state
                };
                this.$http.post("/product", param).then(res => {
                    let {data, totals} = res.data;
                    this.products = data;
                    this.total = totals;
                });
            },
            //高级查询按钮
            searchFormProducts(){
                //把页码重新设置到第一页
                this.currentPage = 1;
                //再次进行查询部门
                this.loadProducts();
            },
            //点击新增按钮触发事件
            handleAdd(){
                //清空校验
                //如果有校验，才清空校验
                if(this.$refs["form"]){
                    this.$refs["form"].resetFields();
                }
                //修改标题
                this.title = "新增";
                //清空form表单中的数据
                for(var k in this.form){
                    this.form[k] = "";
                }
                this.fileList = [];
                //弹出窗体
                this.addFormVisible = true;
            },
            //保存/修改都是用此事件
            addSubmit(){
                //创建服务详情对象
                var productDetail = {"orderNotice": this.form.orderNotice, "intro": this.form.intro};
                //给服务对象设置属性
                this.form.productDetail = productDetail;

                //在提交之前，form表单必须要先经过校验
                this.$refs["form"].validate((valid) => {
                    if (valid) {
                        //保存/修改 数据
                        this.$http.put("/product", this.form).then(res => {
                            let {success, message} = res.data;

                            if(success){
                                //关闭窗体
                                this.addFormVisible = false;
                                //刷新界面
                                this.loadProducts();
                            }else{
                                this.$message.error(message);
                            }
                        });
                    }
                });


            }


        },
        mounted(){
            //调用分页列表
            this.loadProducts();
        }
    };
</script>